<?php

class Stat_productApp extends BackendApp {

    var $search_arr;
    var $_product_statistics_mod;

    function __construct() {
        $this->Stat_memberApp();
    }

    function Stat_memberApp() {
        parent::__construct();
        $this->_product_statistics_mod = &m("product_statistics");
        import('datehelper.lib');
        import('statistics.lib');
        $this->import_resource(
            array(
                'script' => 'highcharts/highcharts.js,statistics.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
                'style' => 'jquery.ui/themes/ui-lightness/jquery.ui.css')
        );
        $this->search_arr = $_REQUEST;
        //处理搜索时间
        if (in_array($_REQUEST['act'], array('statistics'))) {
            $this->search_arr = dealwithSearchTime($this->search_arr);
            //获得系统年份
            $year_arr = getSystemYearArr();
            //获得系统月份
            $month_arr = getSystemMonthArr();
            //获得本月的周时间段
            $week_arr = getMonthWeekArr($this->search_arr['week']['current_year'], $this->search_arr['week']['current_month']);
            $this->assign('year_arr', $year_arr);
            $this->assign('month_arr', $month_arr);
            $this->assign('week_arr', $week_arr);
        }
        $this->assign('actionurl', 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING']);
        $this->assign('search_arr', $this->search_arr);
    }

    /**
     * 查询每月的周数组
     */
    public function getweekofmonth() {
        $year = $_GET['y'];
        $month = $_GET['m'];
        $week_arr = getMonthWeekArr($year, $month);
        echo json_encode($week_arr);
        die;
    }

    //uv统计数值
    function statistics() {

        ini_set('display_errors','on');
        error_reporting(E_ALL);


        $statlist = array(); //统计数据列表
        //新增总数数组
        $count_arr = array('up' => 0, 'curr' => 0);
        $condition = '';
        if($_GET['search_time'] && !$_GET['exporttype']){
            $stime = $this->search_arr['day']['search_time'] - 86400; //昨天0点
            $etime = $this->search_arr['day']['search_time'] + 86400 - 1; //今天24点
            $condition .= " AND create_time <= {$etime} AND create_time >= {$stime}";
        }

        $statlist['headertitle'] = array('ID','时间', 'Spu数量', 'Sku数量', '自有库存数量','代销库存数量','自有库存金额','代销库存金额','供应商数量','平均单价');
        $statlist['data'] = $this->_product_statistics_mod->find(array(
            'fields' => '*',
            'conditions' => "1 = 1".$condition,
            'order'         => "create_time desc",
        ));


        //导出Excel
        if ($_GET['exporttype'] == 'excel') {
            $to_charset = (CHARSET == 'utf-8') ? substr(LANG, 0, 2) == 'sc' ? 'gbk' : 'big5' : '';
            $num = 0;
            $data[$num]['ID'] = $statlist['headertitle'][0];
            $data[$num]['spu_count'] = $statlist['headertitle'][2];
            $data[$num]['sku_count'] = $statlist['headertitle'][3];
            $data[$num]['self_stock_sum'] = $statlist['headertitle'][4];
            $data[$num]['agent_stock_sum'] = $statlist['headertitle'][5];
            $data[$num]['self_stock_price_total'] = $statlist['headertitle'][6];
            $data[$num]['agent_stock_price_total'] = $statlist['headertitle'][7];
            $data[$num]['vendor_count'] = $statlist['headertitle'][8];
            $data[$num]['avg_price'] = $statlist['headertitle'][9];
            $data[$num]['create_time'] = $statlist['headertitle'][1];
            foreach($statlist['data'] as $k=>$v){
                $data[$num+1]['ID'] = $v['id'];
                $data[$num+1]['spu_count'] = $v['spu_count'];
                $data[$num+1]['sku_count'] = $v['sku_count'];
                $data[$num+1]['self_stock_sum'] = $v['self_stock_sum'];
                $data[$num+1]['agent_stock_sum'] = $v['agent_stock_sum'];
                $data[$num+1]['self_stock_price_total'] = $v['self_stock_price_total'];
                $data[$num+1]['agent_stock_price_total'] = $v['agent_stock_price_total'];
                $data[$num+1]['vendor_count'] = $v['vendor_count'];
                $data[$num+1]['avg_price'] = $v['avg_price'];
                $data[$num+1]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
                $num++;
            }
            if(!empty($data)){
                $this->export_to_csv($data, date('Y-m-d H:i:s').'库存数统计', $to_charset);
                exit();
            }
        }
        $this->assign('actionurl', 'index.php?app=stat_product&act=statistics&search_type=day&search_time=' . date('Y-m-d', $this->search_arr['day']['search_time']));
        $this->assign('statlist', $statlist);
        $this->assign('filtered', $condition? 1 : 0); //是否有查询条件
        $this->display('stat_product.statistics.html');
        exit;
    }

}

?>
